<?php
//echo implode('~', $_POST);
require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.exchange_rates.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.airports.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.lurls.php";
	
// Start Sessions
session_start();
header("Cache-control: private");
		

if(isset($_POST['holiday_search'])){
   
	// Clear old search data
	unset($_SESSION['holiday_search_response'],$_SESSION['holiday_search_params']);
	
	$holidays = new holidays();
	// Prepare search criteria
	$params = preprocess_search_form($_POST);
        

	$_SESSION['holiday_search_params'] = $params;        
	// Create the request string
	$request = $holidays->create_grq_query($params);
        	
	
	// Create the request string
	$request = $holidays->create_grq_query($params);

	// Get the results
	$key = md5("micros_params_".implode(',',$params));
	
	
	$memcache = new Memcache;
	foreach(unserialize(CHOSTS) as $cache_server){
		 $memcache->addServer($cache_server, 11211);
	}
	
	
	//lookup value in memcache
	$result = $memcache->get($key);
			 
	if($result){
		$holiday_search_response = $result;
	}else{
		$holiday_search_response = $holidays->connection_sabs($request);
		// Set cache
		$memcache->set($key,$holiday_search_response,0,600);
	}
	
	unset($params);
	
	
	// Get the results
	$_SESSION['holiday_search_response'] = $holiday_search_response;
	// Remove
	//file_put_contents(SITE_ROOT_PATH."/_lib/logs/".$time."-response.txt", $_SESSION['holiday_search_response']);
	setcookie("holiday_search_string", implode('~', $_POST), 0, "/");
		
    // redirect browser to search results page
    // avoid search repeat when refreshing browser
    header("Location: results_group.php");
    exit;
	
}elseif($_SESSION['holiday_search_response']){


	$holidays = new holidays();
	$airports = new airports();
	$destinations = new destinations();
	$lurls = new lurls();
	$tree = new tree();
	
	$holiday_search_response = $_SESSION['holiday_search_response'];
	$holiday_search_params = $_SESSION['holiday_search_params'];

	// get the destination values
	$params['tree_id'] = $holiday_search_params['destination_tree_id'];
	$params['cache']['seconds'] = '600';
	$destination = $tree->get_branch($params);
	$destination = $destination[0];
	unset($params);

	
	// Template
	$template = new template();
	$template->title = $destination['name']." last minute holidays from Ireland";
	$template->meta_keywords = $destination['name'].", holidays, last minute holidays, from Ireland, cheap holidays";
	$template->meta_description = "Book last minute holidays to ".$destination['name']." from Ireland > Dublin, Cork, Belfast and Shannon airports.";
	$template->section_highlight = "holidays";
	$template->search_tab = "holidays";
	$template->left_col = true;
	$template->breadcrumbs_html = "<li>&rsaquo;</li><li title=\"Holidays\"><a href=\"/holidays/index.html\" title=\"Holidays\">Holidays</a></li><li>&rsaquo;</li><li title=\"Holiday Search Results\"><a href=\"/holidays/results_group.html\" title=\"Holidays Search Results\">Search Results</a></li>";
	$template->ld_head();
	

	

?>



<h2 title="Holiday Search Results">Holiday Search Results</h2>


<?

require_once SITE_ROOT_PATH."/_lib/html/holiday_results_group.php";


	$template->ld_foot();

	
	
}else{
	// If not found then send to 404 error
	header("HTTP/1.0 404 Not Found"); 
	include($_SERVER['DOCUMENT_ROOT']."/404.php");
	exit;
}
	








function preprocess_search_form($params) {

	$holiday_search = array();
	
	$holiday_search['give_take'] = $params['give_take'];
	$holiday_search['destination_tree_id'] = $params['destination_tree_id'];
        $holiday_search['destination_tree_master_id'] = $params['destination_tree_master_id_holidays'];
	// Get airport ISO code
	if(!empty($params['destination_tree_id'])){
		$d_params['tree_id'] = $params['destination_tree_id'];
		$tree = new tree();
		$destination = $tree->get_branch($d_params,1);
		$destination = $destination[0];
		unset($d_params);
		$holiday_search['destination_airport'] = $destination['airport_code_search'];
	}else{
		$holiday_search['destination_airport'] = "";
	}
	
	$holiday_search['nights'] = $params['nights'];
	$holiday_search['departure_airport'] = $params['departure_airport'];
	
	if(empty($params['pax_adult'])){
		$holiday_search['pax_adult'] = 2;
	}else{
		$holiday_search['pax_adult'] = $params['pax_adult'];
	}
	
	if(empty($params['pax_child'])){
		$holiday_search['pax_child'] = 0;
	}else{
		$holiday_search['pax_child'] = $params['pax_child'];
	}

	$departure_date = $params['date_day']."-".$params['date_month'];
	$dd = substr($departure_date, 0, 2); 
	$dm = substr($departure_date, 3, 2); 
	$dy = substr($departure_date, 8, 2); 
	
	if(!checkdate($dm,$dd,$dy)){
		$holiday_search['departure_date'] = $dy.$dm."28";
	}else{
		$holiday_search['departure_date'] = $dy.$dm.$dd;
	}
    
    return $holiday_search;
}



